package jmu.cyxwcy.librarymanagement.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jmu.cyxwcy.librarymanagement.mapper.BorrowingMapper;
import jmu.cyxwcy.librarymanagement.pojo.*;
import jmu.cyxwcy.librarymanagement.service.BookDescriptionService;
import jmu.cyxwcy.librarymanagement.service.IBorrowingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-25
 */
@RestController
@RequestMapping("/borrowing")
public class BorrowingController {
    @Autowired
    private IBorrowingService iBorrowingService;


    //传入bookId,user_id就可以了，其他由数据库自动生成
    @PostMapping("/add")
    public Result addBorrowing(@RequestBody Borrowing borrowing){
        iBorrowingService.getBaseMapper().insert(borrowing);
        return Result.success();
    }


    @PutMapping("/update")
    public Result updateBorrowing(@RequestBody Borrowing borrowing){
        iBorrowingService.updateById(borrowing);
        return Result.success();
    }

    @DeleteMapping("/delete/{borrowingId}")
    public Result deleteBorrowing(@PathVariable Integer borrowingId){
        iBorrowingService.removeById(borrowingId);
        return Result.success();
    }

    //图书归还
    @PutMapping("/updateReturnDate")
    public Result updateReturnDate(@RequestParam Integer borrowingId){
        iBorrowingService.updateReturnDate(borrowingId);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<IPage<Borrowing>> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") Integer userId,
                                        @RequestParam(defaultValue = "") String categoryName,
                                        @RequestParam(defaultValue = "") Integer bookId,
                                        @RequestParam(defaultValue = "") String bookName,
                                        @RequestParam(defaultValue = "") String studentName,
                                        @RequestParam(defaultValue = "") String teacherName) {
        IPage<Borrowing> page = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<Borrowing> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Borrowing.class)
                .selectAs(ReaderCategories::getCategoryName, Borrowing::getCategoryName)
                .selectAll(Book.class)
                .selectAll(BookDescription.class)
                .selectAll(Students.class)
                .selectAll(Teachers.class)
                .leftJoin(User.class,User::getUserId, Borrowing::getUserId)
                .leftJoin(ReaderCategories.class,ReaderCategories::getCategoryId, User::getCategoryId)
                .leftJoin(Book.class,Book::getBookId, Borrowing::getBookId)
                .leftJoin(BookDescription.class,BookDescription::getIsbn, Book::getIsbn)
                .leftJoin(Students.class,Students::getUserId,User::getUserId)
                .leftJoin(Teachers.class,Teachers::getUserId,User::getUserId)
                .like(userId!=null, Borrowing::getUserId, userId)
                .like(!categoryName.isEmpty(), ReaderCategories::getCategoryName,categoryName)
                .like(bookId!=null, Book::getBookId, bookId)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(!studentName.isEmpty(), Students::getStudentName, studentName)
                .like(!teacherName.isEmpty(), Teachers::getTeacherName, teacherName);
        queryWrapper.orderByAsc("borrowing_id");
        IPage<Borrowing> pg= iBorrowingService.join(page, queryWrapper);
        return Result.success(pg);
    }

    @GetMapping("/page/students")
    public Result<IPage<Borrowing>> findPageForStudents(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "") String categoryName,
            @RequestParam(defaultValue = "") String bookName,
            @RequestParam(defaultValue = "") String studentName,
            @RequestParam(defaultValue = "") String paymentStatus,
//            @RequestParam(defaultValue = "") String borrowDate,
//            @RequestParam(defaultValue = "") String returnDate,
//            @RequestParam(defaultValue = "") String dueDate,
            @RequestParam(defaultValue = "") String renewalStatus,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer studentId,
            @RequestParam(required = false) Integer bookId,
            @RequestParam(required = false) Integer classId,
            @RequestParam(required = false) Integer collegeId,
            @RequestParam(required = false) Integer majorId,
            @RequestParam(required = false) Integer borrowingId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "YYYY-MM-DD") LocalDate semesterStartDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "YYYY-MM-DD") LocalDate semesterEndDate
    ) {
        IPage<Borrowing> page = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<Borrowing> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Borrowing.class)
                .selectAs(ReaderCategories::getCategoryName, Borrowing::getCategoryName)
                .selectAll(Book.class)
                .selectAll(Classes.class)
                .selectAll(BookDescription.class)
                .selectAll(Students.class)
                .selectAll(Fines.class)
                .selectAll(Major.class)
                .selectAll(College.class)
                .leftJoin(Fines.class, Fines::getBorrowingId, Borrowing::getBorrowingId)
                .leftJoin(User.class, User::getUserId, Borrowing::getUserId)
                .leftJoin(ReaderCategories.class, ReaderCategories::getCategoryId, User::getCategoryId)
                .leftJoin(Book.class, Book::getBookId, Borrowing::getBookId)
                .leftJoin(BookDescription.class, BookDescription::getIsbn, Book::getIsbn)
                .leftJoin(Students.class, Students::getUserId, User::getUserId)
                .leftJoin(Classes.class, Classes::getClassId, Students::getClassId)
                .leftJoin(Major.class, Major::getMajorId, Classes::getMajorId)
                .leftJoin(College.class,College::getCollegeId,Major::getCollegeId)
                .eq(userId != null, Borrowing::getUserId, userId)
                .ne(ReaderCategories::getCategoryName, "教师")
                .like(bookId != null, Book::getBookId, bookId)
                .like(categoryName != null, ReaderCategories::getCategoryName, categoryName)
                .like(studentId != null, Students::getStudentId, studentId)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(!studentName.isEmpty(), Students::getStudentName, studentName)
                .like(borrowingId!=null, Borrowing::getBorrowingId, borrowingId)
                .like(!renewalStatus.isEmpty(), Borrowing::getRenewalStatus, renewalStatus)
                .like(!paymentStatus.isEmpty(), Fines::getPaymentStatus, paymentStatus)
//                .eq(!borrowDate.isEmpty(),Borrowing::getBorrowDate,borrowDate)
//                .eq(!returnDate.isEmpty(),Borrowing::getReturnDate,returnDate)
//                .eq(!dueDate.isEmpty(),Borrowing::getDueDate,dueDate)
                .eq(classId != null, Students::getClassId, classId)
                .eq(collegeId != null, Major::getCollegeId, collegeId)
                .eq(majorId != null, Classes::getMajorId, majorId);
        if (semesterStartDate != null && semesterEndDate != null) {
            queryWrapper.between(Borrowing::getBorrowDate, semesterStartDate, semesterEndDate);
        }
        queryWrapper.orderByAsc(Borrowing::getBorrowingId);
        IPage<Borrowing> pg = iBorrowingService.join(page, queryWrapper);
        return Result.success(pg);
    }

    @GetMapping("/page/teachers")
    public Result<IPage<Borrowing>> findPageForTeachers(@RequestParam Integer pageNum,
                                                        @RequestParam Integer pageSize,
                                                        @RequestParam(defaultValue = "") Integer userId,
                                                        @RequestParam(defaultValue = "") String categoryName,
                                                        @RequestParam(defaultValue = "") Integer bookId,
                                                        @RequestParam(defaultValue = "") String bookName,
                                                        @RequestParam(defaultValue = "") String teacherName) {
        IPage<Borrowing> page = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<Borrowing> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Borrowing.class)
                .selectAs(ReaderCategories::getCategoryName, Borrowing::getCategoryName)
                .selectAll(Book.class)
                .selectAll(BookDescription.class)
                .selectAll(Teachers.class)
                .selectAll(Fines.class)
                .leftJoin(Fines.class, Fines::getBorrowingId, Borrowing::getBorrowingId)
                .leftJoin(User.class, User::getUserId, Borrowing::getUserId)
                .leftJoin(ReaderCategories.class, ReaderCategories::getCategoryId, User::getCategoryId)
                .leftJoin(Book.class, Book::getBookId, Borrowing::getBookId)
                .leftJoin(BookDescription.class, BookDescription::getIsbn, Book::getIsbn)
                .leftJoin(Teachers.class, Teachers::getUserId, User::getUserId)
                .like(userId != null, Borrowing::getUserId, userId)
                .eq(ReaderCategories::getCategoryName, "教师")
                .like(bookId != null, Book::getBookId, bookId)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(!teacherName.isEmpty(), Teachers::getTeacherName, teacherName);
        queryWrapper.orderByAsc(Borrowing::getBorrowingId);
        IPage<Borrowing> pg = iBorrowingService.join(page, queryWrapper);
        return Result.success(pg);
    }

}
